import {
  createSlice
} from "@reduxjs/toolkit";
import axios from "axios";

const channelStore = createSlice({
  name:'channel',
  initialState:{
    channelList:[]
  },
  reducers:{
    //同步方法
    getChannelList(state,action){
      state.channelList=action.payload
    }
  }
})
const {getChannelList}=channelStore.actions
//封装异步方法
const fetchChannelList=()=>{
  return async (dispatch)=>{
    const res=await axios.get('http://geek.itheima.net/v1_0/channels')
    dispatch(getChannelList(res.data.data.channels))
  }
}
const channelReducer=channelStore.reducer
export {fetchChannelList}
export default channelReducer